home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15290 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.4 KB  |  102 lines

  1. Newsgroups: comp.lang.c++
  2. Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!atluw04!not-for-mail
  3. From: Guru Chandar <GCHAN@dbsoftware.com>
  4. Subject: Assignment op. for an object with reference member
  5. Message-ID: <3163FE18.28D@dbsoftware.com>
  6. Date: Thu, 04 Apr 1996 11:51:36 -0500
  7. Organization: Dun & Bradstreet Software
  8. X-Mailer: Mozilla 2.0GoldB1 (WinNT; I)
  9. MIME-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12.  
  13. I ran into a situation where I was asked to write an assignment operator
  14. for a class that has a reference to another class as a data member.
  15. The requirement was that after the assignment, the reference on the target object
  16. must refer to the same object that the reference on the source does.
  17.  
  18. I have given an example below.
  19.  
  20. #include <iostream.h>
  21.  
  22. class A
  23. {
  24. public:
  25.     A(int x) :x_(x){}
  26.     friend ostream& operator<<(ostream& os,const A& a)
  27.                 {os << "x_ = " << a.x_; return os;}
  28. private:
  29.     int x_;
  30. };
  31.  
  32. class B 
  33. {
  34. public:
  35.     B(A& a) : a_(a) {}
  36.     B(B& src): a_(src.a_){}
  37.     B& operator=(B& src);
  38.  
  39.     friend ostream& operator<<(ostream& os,const B& b)
  40.                 {os << b.a_ << endl; return os;}
  41. private:
  42.     A& a_;
  43. };
  44.  
  45. B& 
  46. B::operator=(B& src)
  47. {
  48.    if (this == &src)
  49.        return *this;
  50.  
  51.    // a_ = src.a_ won't work, it will change the value of what a_ refers to!
  52.  
  53.    //Invoke the copy constructor on this object
  54.    this->B::B(src);   // if you take out this-> the behavior changes on VC++ 4.0
  55.  
  56.    return *this;
  57. }
  58.  
  59. void main()
  60. {
  61.    A a1(1);
  62.    A a2(2);
  63.  
  64.    B b1(a1);
  65.    B b2(a2);
  66.  
  67.    cout << "b1 = " << b1;
  68.    cout << "b2 = " << b2;
  69.  
  70.    b1 = b2;  // after this b1.a_ should refer to b2.a_ i.e a2
  71.  
  72.    cout << "b1 = " << b1;
  73.    cout << "b2 = " << b2;
  74. }
  75.  
  76. When I run this on VC++ 4.0
  77. The output is :
  78. b1 = x_ = 1
  79. b2 = x_ = 2
  80. b1 = x_ = 2
  81. b2 = x_ = 2
  82.  
  83. But on HP C++ I get a compilation error.
  84.  
  85. I have two questions:
  86.  
  87. 1. Is it legel to even attempt to change a reference after it is initialized?
  88.    ARM & other books say that the only way to assign a value to a reference
  89.    is when you initialize it.
  90.  
  91. 2. If the answer to Q1 is Yes, then is the code given above, especially the
  92.    syntax to invoke the copy constructor explicitly on an object: 
  93.  
  94.      this->B::B(src); // if you take out this-> the behavior changes on VC++ 4.0
  95.    
  96.    valid? Is it supported in the standard? 
  97.  
  98. Any advice, thoughts on this will be appreciated.
  99.  
  100. --------------------------------------------------------------------------------
  101.  
  102.